package HGclient;

import java.awt.Color;
import java.text.SimpleDateFormat;
import java.util.Vector;



public class GrabHGclientThread extends Thread{

	@Override
    public void run() {
		
		String address = "http://66.133.87.54/";
		
		//String address = "http://hga020.com/";
		
		int times = 0;
		
		
		try{

				HGclienthttp hg = new HGclienthttp();
				
				HGclienthttp.recoverGamedetailsVecfromefile();
				
				hg.setLoginParams(address, "gcwfool5", "aaa222");
				
				int b = hg.login();
				//Thread.sleep(500);
				if(b == 1){
					
					
				}else{
					hg.login();
				}
				
				SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				
				
				int failtimes = 0;
				
				while(true){
					
					times++;
					
					boolean bgettotal = hg.getTotalBet();
					
					//Thread.sleep(500);
					
					
					if(bgettotal){
						//BetRatioManager.constructBetvecs();
					}
					
					
					boolean bgetinplay = hg.getInplaybet();
					
					//Thread.sleep(500);
					
					boolean bgetgameres = true;
					
					if(times > 50){
						bgetgameres = hg.getgameresult();
						times = 0;
					}
					
					//Thread.sleep(500);
					
					if(bgettotal && bgetinplay && bgetgameres){
						hg.removelastdayevents();
						
						hg.updateGamedetailsvec();
						
						hg.updateDXQdetailsvec();
						
						//System.out.println("get all success:" + df.format(System.currentTimeMillis()));
						
						HGclienthttp.setGrabStext("更新成功");
						
						
						HGclienthttp.setGrabColor(Color.GREEN);
						
						HGclienthttp.setdxqGrabStext("更新成功");
						
						
						HGclienthttp.setdxqGrabColor(Color.GREEN);
						
						
						
						Thread.currentThread();
						Thread.sleep(600);
						
						
					}else{
						
						System.out.println(Boolean.toString(bgettotal) + bgetinplay + bgetgameres);
						
						
						if(hg.getIsNeedRelogin() == true){
							
							
							HGclienthttp.setGrabColor(Color.RED);
							HGclienthttp.setdxqGrabColor(Color.RED);
							
							
							System.out.println("failed:" + df.format(System.currentTimeMillis()));
							
							//hg.setLoginParams(address, "gcwfool5", "aaa222");
							
							
							int loginres = hg.login();
							
							
							
							while(loginres != 1){
								
								if(loginres == 2){
									HGclienthttp.setGrabStext("维护中...");
									HGclienthttp.setdxqGrabStext("维护中...");
									
									Thread.currentThread();
									Thread.sleep(300*1000);
									loginres = hg.login();
								}else if(loginres == -1){
									Thread.currentThread();
									Thread.sleep(10*1000);
								}else if(loginres == 3){
									Thread.currentThread();
									Thread.sleep(10*1000);
									System.out.println("启动备用账号:");
									hg.setLoginParams(address, "gcwfool5", "aaa222");
								}
								
								loginres = hg.login();
								
							}
							
							
							System.out.println("relogin success");
							hg.setIsNeedRelogin(false);
							
							
							
							
							
						}
						
						

						
						
							
							
						
					}
					
					
					

					
				}
				

		}catch(Exception e){
			e.printStackTrace();
		}	
	}
	
}
